package com.hrm;

import java.util.Date;
import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hrm.dao.ActionMapper;
import com.hrm.dao.IconMapper;
import com.hrm.dao.MenuMapper;
import com.hrm.dao.RoleActionMapper;
import com.hrm.dao.RoleMapper;
import com.hrm.dao.UserMapper;
import com.hrm.dao.UserRoleMapper;
import com.hrm.entity.Action;
import com.hrm.entity.Icon;
import com.hrm.entity.Menu;
import com.hrm.entity.Role;
import com.hrm.entity.RoleAction;
import com.hrm.entity.User;
import com.hrm.entity.UserRole;

public class InitData {
	 static SqlSession session=null;
	static{
		 //读取spring配置文件
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "spring.xml");
        //通过spring配置文件生成的beanFactory获得SqlSessionFactory
        SqlSessionFactory factory = (SqlSessionFactory) beanFactory
                .getBean("sqlSessionFactory");
        //获得SqlSession
        session = factory.openSession();
	}
	public static void main(String[] args) {
		//插入Menu数据
		UserMapper userMapper = session.getMapper(UserMapper.class);
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
		MenuMapper menuMapper = session.getMapper(MenuMapper.class);
		ActionMapper actionMappper = session.getMapper(ActionMapper.class);
		RoleActionMapper roleActionMapper = session.getMapper(RoleActionMapper.class);
		IconMapper iconMapper = session.getMapper(IconMapper.class);
		
		//清空数据库
		menuMapper.deleteMenuByMap(new HashMap<String, Object>());
		actionMappper.deleteActionByMap(new HashMap<String, Object>());
		roleMapper.deleteRoleByMap(new HashMap<String, Object>());
		userMapper.deleteUserByMap(new HashMap<String, Object>());
		userRoleMapper.deleteUserRoleByMap(new HashMap<String, Object>());
		roleActionMapper.deleteRoleActionByMap(new HashMap<String, Object>());
//		iconMapper.deleteIconByMap(new HashMap<String, Object>());
		
		//系统管理员数据
		User user = new User();
		user.setUserName("admin");
		user.setPassword("admin");
		user.setCreateUserName("admin");
		user.setCreateDate(new Date());
		userMapper.insertUser(user);
		user.setCreateUserId(user.getId());
		userMapper.updateUser(user);
		
		//系统管理员角色数据
		Role role =new Role();
		role.setRoleDesc("admin");
		role.setRoleName("系统管理员");
		role.setUserId(user.getId());
		role.setUserName(user.getUserName());
		role.setCreateDate(new Date());
		roleMapper.insertRole(role);
		int sysRoleId=role.getId();
		
		Role everyRole =new Role();
		everyRole.setRoleDesc("everyone");
		everyRole.setRoleName("基础角色");
		everyRole.setUserId(user.getId());
		everyRole.setUserName(user.getUserName());
		everyRole.setCreateDate(new Date());
		roleMapper.insertRole(everyRole);
		int everyOneRoleId=everyRole.getId();
		
		//用户和角色关系对应表
		UserRole userRole = new UserRole();
		userRole.setRoleId(sysRoleId);
		userRole.setUserId(user.getId());
		userRole.setCreateUserId(user.getId());
		userRole.setCreateUserName(user.getUserName());
		userRole.setCreateDate(new Date());
		userRoleMapper.insertUserRole(userRole);
		
		UserRole everyUserRole = new UserRole();
		everyUserRole.setRoleId(everyOneRoleId);
		everyUserRole.setUserId(user.getId());
		everyUserRole.setCreateUserId(user.getId());
		everyUserRole.setCreateUserName(user.getUserName());
		everyUserRole.setCreateDate(new Date());
		userRoleMapper.insertUserRole(everyUserRole);
		
		
		//工作桌面数据
		Menu menu = new Menu();
		menu.setMenuName("工作桌面");
		menu.setViewMode("Y");
		menu.setId(0);
		menuMapper.insertMenu(menu);//每次插入一个数据后，会自动将当前插入的对象的ID设置为最新插入的数据的id
		//插入Action数据
		Action action = new Action();
		action.setActionCode("commonOperation");
		action.setActionName("常用操作");
		action.setMenuId(menu.getId());
		action.setPath("jsp/workspace/Welcome.jsp");
		action.setIcon("images/icons/icon028a1.gif");
		action.setViewMode("Y");
		actionMappper.insertAction(action);
		
		RoleAction roleAction = new RoleAction();
		roleAction.setRoleId(role.getId());
		roleAction.setUserId(user.getId());
		roleAction.setUserName(user.getUserName());
		roleAction.setCreateDate(new Date());
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		RoleAction everyRoleAction = new RoleAction();
		everyRoleAction.setRoleId(everyOneRoleId);
		everyRoleAction.setUserId(user.getId());
		everyRoleAction.setUserName(user.getUserName());
		everyRoleAction.setCreateDate(new Date());
		everyRoleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(everyRoleAction);
		
		//组织机构数据
		menu.setId(0);//所以需要将id重新设置为0
		menu.setMenuName("组织机构");
		menuMapper.insertMenu(menu);
		action.setId(0);
		action.setActionCode("groupManage");
		action.setActionName("部门管理");
		action.setPath("jsp/group/GroupManage.jsp");
		action.setIcon("images/icons/icon012a1.gif");
		action.setMenuId(menu.getId());
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("groupDiagram");
		action.setActionName("组织结构图");
		action.setPath("jsp/group/GroupDiagram.jsp");
		action.setIcon("images/icons/icon042a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		//人事管理数据
		menu.setId(0);
		menu.setMenuName("人事管理");
		menuMapper.insertMenu(menu);
		action.setId(0);
		action.setActionCode("employeeInfoManage");
		action.setActionName("员工信息管理");
		action.setPath("jsp/employee/EmployeeInfoManage.jsp");
		action.setIcon("images/icons/icon032a1.gif");
		action.setMenuId(menu.getId());
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("employeeTransferManage");
		action.setActionName("员工调动管理");
		action.setPath("jsp/employee/EmployeeTransferManage.jsp");
		action.setIcon("images/icons/icon010a20.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("employeeTrainManage");
		action.setActionName("员工培训管理");
		action.setPath("jsp/employee/EmployeeTrainManage.jsp");
		action.setIcon("images/icons/icon031a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("employeeTitleManage");
		action.setActionName("员工职称管理");
		action.setPath("jsp/employee/EmployeeTitleManage.jsp");
		action.setIcon("images/icons/icon018a4.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("employeeTotalManage");
		action.setActionName("员工统计信息");
		action.setPath("jsp/employee/EmployeeTotalManage.jsp");
		action.setIcon("images/icons/icon031a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		//绩效管理数据
		menu.setId(0);
		menu.setMenuName("绩效管理");
		menuMapper.insertMenu(menu);
		action.setId(0);
		action.setActionCode("employeeR&PManage");
		action.setActionName("员工奖惩管理");
		action.setMenuId(menu.getId());
		action.setPath("jsp/employee/EmployeeR&PManage.jsp");
		action.setIcon("images/icons/icon030a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("employeeAppraisalManage");
		action.setActionName("员工考评管理");
		action.setPath("jsp/employee/EmployeeAppraisalManage.jsp");
		action.setIcon("images/icons/icon012a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		
		//福利发放数据
		menu.setId(0);
		menu.setMenuName("福利发放");
		menuMapper.insertMenu(menu);
		action.setId(0);
		action.setActionCode("welfareGrant");
		action.setActionName("福利物品发放");
		action.setMenuId(menu.getId());
		action.setPath("jsp/reward/RewardPublish.jsp");
		action.setIcon("images/icons/icon007a10.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("welfareManage");
		action.setActionName("福利物品维护");
		action.setPath("jsp/reward/RewardManage.jsp");
		action.setIcon("images/icons/icon029a7.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		
		//薪酬管理数据
		menu.setId(0);
		menu.setMenuName("薪酬管理");
		menuMapper.insertMenu(menu);
		action.setId(0);
		action.setActionCode("salaryAccountManage");
		action.setActionName("工资帐套管理");
		action.setMenuId(menu.getId());
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("userAccountManage");
		action.setActionName("员工帐套管理");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("salaryTableManage");
		action.setActionName("工资表管理");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("salaryByPManage");
		action.setActionName("计件工资");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("salaryByHManage");
		action.setActionName("计时工资");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("totalSalaryTable");
		action.setActionName("工资表汇总");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("operationAtMonthEnd");
		action.setActionName("月末处理");
		actionMappper.insertAction(action);
//		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		menu.setId(0);
		menu.setMenuName("系统管理");
		menuMapper.insertMenu(menu);
		action.setId(0);
		action.setActionCode("systemInfoManage");
		action.setActionName("基本资料维护");
		action.setMenuId(menu.getId());
		action.setPath("jsp/system/SystemInfoManage.jsp");
		action.setIcon("images/icons/icon002a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("permissionConfig");
		action.setActionName("权限配置");
		action.setPath("jsp/system/PermissionConfig.jsp");
		action.setIcon("images/icons/icon042a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("userManage");
		action.setActionName("用户管理");
		action.setPath("jsp/system/UserManage.jsp");
		action.setIcon("images/icons/icon021a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("menuManage");
		action.setActionName("菜单管理");
		action.setPath("jsp/system/MenuManage.jsp");
		action.setIcon("images/icons/icon021a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("roleManage");
		action.setActionName("角色管理");
		action.setPath("jsp/system/RoleManage.jsp");
		action.setIcon("images/icons/icon025a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("roleManage");
		action.setActionName("配置项管理");
		action.setPath("jsp/system/ConfigManage.jsp");
		action.setIcon("images/icons/icon018a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("personNote");
		action.setActionName("个人备忘");
		action.setPath("jsp/system/PersonNote.jsp");
		action.setIcon("images/icons/icon016a1.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("databack");
		action.setActionName("数据备份");
		action.setPath("jsp/system/DataBack.jsp");
		action.setIcon("images/icons/icon023a6.gif");
		actionMappper.insertAction(action);
		
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		action.setId(0);
		action.setActionCode("initSystem");
		action.setActionName("系统初始化");
		
		actionMappper.insertAction(action);
		action.setPath("jsp/system/SystemInit.jsp");
		action.setIcon("images/icons/icon018a6.gif");
		roleAction.setId(0);
		roleAction.setActionId(action.getId());
		roleActionMapper.insertRoleAction(roleAction);
		
		//Icon数据维护
		
//		for(int i=0;i<52;i++){
//			Icon parentIcon = new Icon();
//			String string="";
//			if(i<10){
//				string="0"+i;
//			}else{
//				string=""+i;
//			}
//			parentIcon.setPath("images/icons/icon0"+string+"a1.gif");
//			parentIcon.setTitle("title0"+string+"a1");
//			parentIcon.setParentId(-1);
//			iconMapper.insertIcon(parentIcon);
//			for(int j=2;j<17;j++){
//				Icon childIcon = new Icon();
//				childIcon.setParentId(parentIcon.getId());
//				childIcon.setTitle("title0"+string+"a"+j);
//				childIcon.setPath("images/icons/icon0"+string+"a"+j+".gif");
//				iconMapper.insertIcon(childIcon);
//			}
//		}
	}

}
